package sdu.icat.forum.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import sdu.icat.forum.service.ForumDocumentService;
import sdu.icat.forum.smodel.ForumComment;
import sdu.icat.forum.smodel.ForumDocument;
import sdu.icat.forum.smodel.ForumReply;
import sdu.icat.pj.base.ResponseMap;

@Api(value="forum view",tags={"forum"})
@RestController
@RequestMapping("/forum")
public class ForumController {

    @Autowired private ForumDocumentService documentService;

    @ApiOperation(value="查看所有文章")
    @PutMapping("/doc/list")
    public ResponseMap getDocument(int page,int size,@RequestBody Map<String,Object> params) {
	ResponseMap result = ResponseMap.newInstance();
	Page<ForumDocument> doclist = documentService.getAllForumDocument(page, size,params);
	result.setSuccessMap("success", doclist);
	return result;
    }

    /**
     * @param url 跳转url
     * @param document
     * @return
     */
    @ApiOperation(value="发表文章")
    @PostMapping("/doc")
    public ResponseMap pubDocument(String url,@RequestBody ForumDocument document) {
	ResponseMap result = ResponseMap.newInstance();
	documentService.publishDocument(document);
	result.setSuccessMap("success", url);
	return result;
    }

    @ApiOperation(value="删除文章")
    @DeleteMapping("/doc")
    public ResponseMap deleteDocument(long[] ids) {
	ResponseMap result = ResponseMap.newInstance();
	int num = documentService.deleteDocument(ids);
	if(num==0) {
	    result.setFailedMap("error", null);
	} else
	    result.setSuccessMap("success", num);
	return result;
    }

    /**
     * @param documentid 帖子下
     * @param comment 发表评论
     * @param isRender 是否渲染
     * @return
     */
    @ApiOperation(value="发表评论")
    @PostMapping("/doc/comment")
    public ResponseMap publishComment(long documentid,@RequestBody ForumComment comment,boolean isRender) {
	ResponseMap result = ResponseMap.newInstance();
	documentService.publishComment(documentid, comment);
	result.setSuccessMap("success", isRender);
	return result;
    }

    @ApiOperation(value="回复评论")
    @PostMapping("/comment/reply")
    public ResponseMap publishReply(long commentid,@RequestBody ForumReply reply,boolean isRender) {
	ResponseMap result = ResponseMap.newInstance();
	documentService.publishReply(commentid, reply);
	result.setSuccessMap("success", isRender);
	return result;
    }

    @ApiOperation(value="贴目详情")
    @PutMapping("/doc/detail")
    public ResponseMap DocumentDetail(long documentid,int page,int size) {
	ResponseMap result = ResponseMap.newInstance();
	Map<String,Object> map =
		documentService.ForumDocumentDetail(documentid, page, size);
	result.setSuccessMap("Ok", map);
	return result;
    }

}
